<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        button {
            display: none;
            position: fixed;
            bottom: 50px;
            right: 50px;

        }
    </style>
</head>

<body>

    <div style="height: 1000px;"></div>
    <button>回到顶部</button>
    <script>
        // 
        var btn = document.querySelector('button');

        window.onscroll = function () {
            console.log(1);
            var st = document.documentElement.scrollTop;
            console.log(st);
            if (st >= 100) {
                btn.style.display = 'block'
            } else {
                btn.style.display = 'none'

            }
        }



        // 回到
        btn.onclick = function () {
            // document.documentElement.scrollTop = 0;
            document.documentElement.scrollTo({
                top: 0,
                behavior: "smooth"
            })
        }



    </script>

</body>

</html>